home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BARMENU.ZIP / MENUUNIT.PAS < prev   
Pascal/Delphi Source File  |  1991-09-13  |  4KB  |  167 lines

  1. UNIT MenuUnit;
  2. Interface
  3. USES CRT;
  4. CONST MaxMsgLen = 32;
  5. TYPE
  6.   MessageString = String[MaxMsgLen];
  7.  
  8.   EntryPointer = ^EntryType;
  9.   EntryType = Object
  10.     Prev, Next          : EntryPointer;
  11.     XCor, YCor, ChoiceNo: Integer;
  12.     Message             : MessageString;
  13.     Constructor Init(ipr, inx: EntryPointer;
  14.                       ix, iy, ic: Integer; im: MessageString);
  15.     Procedure Draw(Selected: Boolean);
  16.     Function GetChoice: Integer;
  17.   END;
  18.  
  19.   BBMenu = Object
  20.     XCor, YCor, Wid, Choices: Integer;
  21.     FirstEntry, CurEntry    : EntryPointer;
  22.     Constructor Init(ix, iy, iw: Integer);
  23.     Destructor Done;
  24.     Procedure AddPrompt(im: MessageString);
  25.     Procedure Draw;
  26.     Function GetChoice: Integer;
  27.   END;
  28.  
  29. IMPLEMENTATION
  30.   Constructor BBMenu.Init(ix, iy, iw: Integer);
  31.   BEGIN
  32.     XCor:= ix;
  33.     YCor:= iy;
  34.     Wid:= iw;
  35.     IF Wid > MaxMsgLen THEN Wid:= MaxMsgLen;
  36.     IF XCor + Wid > 80 THEN Wid:= 80 - XCor;
  37.     FirstEntry:= NIL;
  38.     Choices:= 0;
  39.   END;
  40.  
  41.   Destructor BBMenu.Done;
  42.   BEGIN
  43.     IF FIrstEntry <> NIL THEN
  44.     BEGIN
  45.       FirstEntry^.Prev^.Next:= NIL;
  46.       REPEAT
  47.         CurEntry:= FirstEntry;
  48.         FirstEntry:= FirstEntry^.Next;
  49.         Dispose(CurEntry);
  50.       UNTIL FirstEntry = NIL;
  51.     END;
  52.   END;
  53.  
  54.   Procedure BBMenu.AddPrompt(im: MessageString);
  55.   VAR EP: EntryPointer;
  56.   BEGIN
  57.     INC(Choices);
  58.     FillChar(im[Length(im) + 1], Wid - Length(im), ' ' );
  59.     Im[0]:= Char(Wid);
  60.     IF FirstEntry = NIL THEN
  61.       BEGIN
  62.         FirstEntry:= NEW(EntryPointer, INIT(NIL,
  63.                          NIL, XCor, YCor + Choices - 1,
  64.                          Choices, im));
  65.         FirstEntry^.Next:= FirstEntry;
  66.         FirstEntry^.Prev:= FirstEntry;
  67.       END
  68.     ELSE
  69.       BEGIN
  70.         EP:= NEW( EntryPointer, Init(FirstEntry^.Prev,
  71.                   FirstEntry, XCor, YCor + Choices - 1,
  72.                   Choices, Im));
  73.         FirstEntry^.Prev^.Next:= EP;
  74.         FirstEntry^.Prev:= EP;
  75.       END;
  76.     END;
  77.  
  78.   Procedure BBMenu.Draw;
  79.   VAR ro, Co: BYTE;
  80.   BEGIN
  81.     GotoXY(XCor - 1, YCor - 1);      Write(#218);
  82.     FOR Co:= 1 TO Wid DO Write(#196); Write(#191);
  83.     FOR Ro:= YCor TO YCor + Choices - 1 DO
  84.       BEGIN
  85.         GotoXY(XCor - 1, ro);   Write(#179);
  86.         GotoXY(XCor + Wid, ro); Write(#179);
  87.       END;
  88.     GotoXY(XCor - 1, YCor + Choices); Write(#192);
  89.     FOR Co:= 1 to Wid DO Write(#196); Write(#217);
  90.     CurEntry:= FirstEntry;
  91.     REPEAT
  92.       CurEntry^.Draw(False);
  93.       CurEntry:= CurEntry^.Next;
  94.     UNTIL CurEntry = FirstEntry;
  95.   END;
  96.  
  97.   CONST
  98.     KEnter = $000D;   KEsc   = $001B;
  99.     KHome  = $4700;   KEnd   = $4F00;
  100.     KLeft  = $4B00;   KRight = $4D00;
  101.     KDown  = $5000;   KUp    = $4800;
  102.  
  103.   Function BBMenu.GetChoice: Integer;
  104.   VAR
  105.     SaveX, SaveY: Integer;
  106.     Finished: Boolean;
  107.     InChar: Char;
  108.     Inword: Word;
  109.   BEGIN
  110.     SaveX:= WhereX;
  111.     SaveY:= WhereY;
  112.     Draw;
  113.     Finished:= False;
  114.     REPEAT
  115.       CurEntry^.Draw(True);
  116.       InChar:= ReadKey;
  117.       IF (InChar = #0) AND KeyPressed THEN
  118.         BEGIN
  119.           InChar:= ReadKey;
  120.           InWord:= Word(InChar) SHL 8;
  121.         END
  122.       ELSE InWord:= ORD(InChar);
  123.       CurEntry^.Draw(False);
  124.       CASE InWord OF
  125.         KLeft, KUp   : CurEntry:= CurEntry^.Prev;
  126.         KRight, KDown: CurEntry:= CurEntry^.Next;
  127.         KHome        : CurEntry:= FirstEntry;
  128.         KEnd         : CurEntry:= FirstEntry^.Prev;
  129.         KEsc         : BEGIN
  130.                          Finished:= True;
  131.                          GetChoice:= 0;
  132.                        END;
  133.         KEnter       : BEGIN
  134.                          Finished:= True;
  135.                          GetChoice:= CurEntry^.GetChoice;
  136.                        END;
  137.       END;
  138.     UNTIL Finished;
  139.     GotoXY( SaveX, SaveY);
  140.   END;
  141.  
  142.   CONSTRUCTOR EntryType.Init(ipr, inx: EntryPointer;
  143.                              ix, iy, ic: Integer;
  144.                              im: MessageString);
  145.   BEGIN
  146.     Prev    := ipr;
  147.     Next    := inx;
  148.     XCor    := ix;
  149.     YCor    := iy;
  150.     ChoiceNo:= ic;
  151.     Message := im;
  152.   END;
  153.  
  154.   Procedure EntryType.Draw(Selected: Boolean);
  155.   BEGIN
  156.     IF Selected THEN TextAttr:= $70
  157.     ELSE TextAttr:= $07;
  158.     GotoXY(XCor, YCor);
  159.     Write(Message);
  160.   END;
  161.  
  162.   Function EntryType.GetChoice: Integer;
  163.   Begin
  164.     GetChoice:= ChoiceNo;
  165.   END;
  166. END.
  167.